﻿using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
using Dexter.Domain.Model;
using Dexter.Logger.Contracts;
using Dexter.Services;
using Dexter.Web.Mvc.Controllers.Backoffice.Extensions;
using Dexter.Web.Mvc.Extensions;
using Dexter.Web.Mvc.Filters;
using Dexter.Web.Mvc.ViewModel.Backoffice.Binders;
using Dexter.Web.Mvc.ViewModel.Backoffice.ViewsData.HomeController;
using Dexter.Web.Utils;

namespace Dexter.Web.Mvc.Controllers.Backoffice {
	[DexterCallFilter ( false )]
	public class HomeController : BackOfficeControllerBase {
		readonly IPostService postService;
		readonly ICategoryService categoryService;
		
		public HomeController ( IConfigurationService configurationService , IWebUtils webUtils , ILogger logger , IUrlBuilder urlBuilder , IContactService contactService , IPostService postService , ICategoryService categoryService ) : base ( configurationService , webUtils , logger , urlBuilder , contactService ) {
			this.postService = postService;
			this.categoryService = categoryService;
		}

		[AcceptVerbs ( HttpVerbs.Get )]
		[OutputCache ( VaryByParam = "id" , Duration = 600 )]
		public ActionResult Images ( string id ) {
			try {
				var file = string.Format ( "~/Areas/Admin/Resources/Images/{0}" , id );
				file = VirtualPathUtility.ToAppRelative ( file );

				return base.File ( file , id.GetMimeType ( ) );
			}
			catch ( Exception e ) {
				Logger.Error ( e.Message , e );
				return new HttpNotFoundResult ( );
			}
		}

		[AcceptVerbs ( HttpVerbs.Get )]
		[OutputCache ( VaryByParam = "id" , Duration = 600 )]
		public ActionResult Styles ( string id ) {
			try {
				var file = string.Format ( "~/Areas/Admin/Resources/Styles/{0}" , id );
				file = VirtualPathUtility.ToAppRelative ( file );

				return base.File ( file, "text/css" );
			}
			catch ( Exception e ) {
				Logger.Error ( e.Message , e );
				return new HttpNotFoundResult ( );
			}
		}

		[AcceptVerbs ( HttpVerbs.Get )]
		[OutputCache ( VaryByParam = "id" , Duration = 600 )]
		public ActionResult Scripts ( string id ) {
			try {
				var file = string.Format ( "~/Areas/Admin/Resources/Scripts/{0}" , id );

				file = VirtualPathUtility.ToAppRelative ( file );
				return base.File ( file, "text/javascript" );
			}
			catch ( Exception e ) {
				Logger.Error ( e.Message , e );
				return new HttpNotFoundResult ( );
			}
		}

		[AcceptVerbs ( HttpVerbs.Get )]
		[OutputCache ( VaryByParam = "id", Duration = 600 )]
		public ActionResult CkEditor ( string id ) {
			try {
				var file = string.Format ( "~/Areas/Admin/Resources/CkEditor/{0}", id );

				file = VirtualPathUtility.ToAppRelative ( file );
				return base.File ( file, id.GetMimeType ( ) );
			}
			catch (Exception e) {
				Logger.Error ( e.Message, e );
				return new HttpNotFoundResult ( );
			}
		}

		[AcceptVerbs ( HttpVerbs.Get )]
		[Authorize ( Roles = "Poster" )]
		public ActionResult Index() {
			var data = new HomeIndexViewData ( base.WebUtils , base.Configuration , base.UrlBuilder );
			data.Categories = categoryService.GetAllCategories ( );
			data.FuturePosts = postService.GetPublishedPostForSpecifiedPublishDateRange ( 0, 7, DateTime.Now, DateTime.Now.AddDays ( 7 ) );
			data.Post = FastPostBinder.EmptyInstance ( );

			return View ( data );
		}

		[ValidateAntiForgeryToken]
		[AcceptVerbs ( HttpVerbs.Post )]
		[Authorize ( Roles = "Poster" )]
		public ActionResult Index ( FastPostBinder post ) {

			if (!ModelState.IsValid) {
				var data = base.GetViewData ( new HomeIndexViewData ( base.WebUtils , base.Configuration , base.UrlBuilder ) );
				data.Categories = categoryService.GetAllCategories ( );
				data.FuturePosts = postService.GetPublishedPostForSpecifiedPublishDateRange ( 0, 7, DateTime.Now, DateTime.Now.AddDays ( 7 ) );
				data.Post = post;

				return View ( data );
			}

			var category = post.CategoryId.HasValue && post.CategoryId > 0
			               	? categoryService.GetCategoryByKeyWithChilds ( post.CategoryId.Value )
			               	: categoryService.GetDefaultCategoryWithChilds ( );

			Post item = Post.CreateNewPost ( post.Title, category, post.FormattedBody, HttpContext.User.Identity.Name );

			post.ToPost ( item );

			postService.Save ( item );

			return this.ReturnFeedback ( MessageType.Positive, "PostSaved", base.UrlBuilder.BackOffice.PostList );
		}

		
	}
}